class YamlEditor
  attr_accessor :panel,:view
  def initialize pane
    @panel = Wx::Panel::new pane
    @panel.set_sizer Wx::BoxSizer::new(Wx::VERTICAL)
    siz = Wx::BoxSizer::new Wx::HORIZONTAL
    siz.add Wx::StaticText::new @panel,-1,"Config file:"
    siz.add 20,20
    file = Wx::TextCtrl::new @panel
    siz.add file
    siz.add 20,20
    siz.add Wx::Button::new(@panel,7777,"Choose")
    @panel.evt_button(7777) {|evt|
       dlg = Wx::FileDialog.new(@panel, "Choose a config file",
       "", "", "Project Config Files(*.pcfg)|*.pcfg|Object Config Files(*.ocfg)|*.ocfg|Server Config Files(*.scfg)|*.scfg",
       Wx::OPEN, Wx::DEFAULT_POSITION)    #
       dlg.set_directory("#{Dir.getwd}/data")
       dlg.show_modal
       f = dlg.get_path
       file.value = f
     }

    siz.add 20,20
    siz.add Wx::Button::new(@panel,7778,"Load")
    @panel.evt_button(7778) {|evt|
      @view.value = File.open(file.value,"r").readlines.join
    }
    @panel.sizer.add siz
    @view = Wx::TextCtrl::new(@panel,:style=>Wx::TE_MULTILINE)
    @panel.sizer.add @view,Wx::GROW|Wx::ALL,Wx::EXPAND
    siz3 = Wx::BoxSizer::new Wx::HORIZONTAL
    @panel.sizer.add siz3
    siz3.add Wx::Button::new(@panel,7779,"Save")
    @panel.evt_button(7779) {|evt|
      File.open(file.value,"w") {|f|
        f.puts @view.value
      }
      if File.basename(file.value) =~ /.*\.ocfg/ or File.basename(file.value) =~ /.*\.pcfg/
        pane.clear_projects
        pane.load_projects
      end
    }
    siz3.add 20,20
    siz3.add Wx::StaticText::new(@panel,-1,"File name (no path or extension):")
    siz3.add 20,20
    f2=Wx::TextCtrl::new(@panel)
    siz3.add f2
    siz3.add 20,20
    siz3.add Wx::Button::new(@panel,7780,"Save As")
    @panel.evt_button(7780) {|evt|
      file.value =~ /(\..*cfg)/
      File.open("data/"+f2.value+$1,"w") {|f|
        f.puts @view.value
      }
      if File.basename(f2.value) =~ /.*\.ocfg/ or File.basename(f2.value) =~ /.*\.pcfg/
        pane.clear_projects
        pane.load_projects
      end
    }
  end
end